//
//  HMMainViewController.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/9.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
 
        let mainTabBar = HMMainTabBar()
        
        mainTabBar.composeClosure = { () -> Void in
            print("加号按钮被点击了")
        }
        
        self.setValue(mainTabBar, forKey: "tabBar")
        
        setupChildViewControllers()
        
    }
    
    
    private func setupChildViewControllers() {
        
        let homeVC = HMHomeViewController()
        
        // 首页
        setupChildViewController(homeVC, imageName: "tabbar_home", title: "首页")
    
        // 消息
        let messageVC = HMMessageViewController()
        setupChildViewController(messageVC, imageName: "tabbar_message_center", title: "消息")
        
        // 发现
        let discoverVC = HMDiscoverTableViewController()
        setupChildViewController(discoverVC, imageName: "tabbar_discover", title: "发现")
        
        // 我
        let profileVC = HMProfileViewController()
        setupChildViewController(profileVC, imageName: "tabbar_profile", title: "我")
    }
    
    
    private func setupChildViewController(controller: UIViewController, imageName: String
        , title: String) {
            
            
            // 设置tabbar的普通图片
            controller.tabBarItem.image = UIImage(named: imageName)
            
            // 设置tabbar的选中图片,拼接高亮图片名称
            controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            
            // 设置标题
            controller.title = title
            
            // 设置标题颜色
            controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
            
            // 包装一个导航控制器 并添加到tabBarVC里面
//            addChildViewController(UINavigationController(rootViewController: controller))
            let nav = UINavigationController(rootViewController: controller)
            addChildViewController(nav)
    }

}
